package com.ls.lserp.modules.setting.service;

import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.modules.order.dao.OrderDao;
import com.ls.lserp.modules.order.entity.CustomizationContract;
import com.ls.lserp.modules.order.entity.Order;
import com.ls.lserp.modules.order.entity.StyleOperationtings;
import com.ls.lserp.modules.order.service.OrderService;
import com.ls.lserp.modules.setting.dao.CheckProjectDao;
import com.ls.lserp.modules.setting.entity.CheckProjectEntity;
import com.ls.lserp.modules.setting.entity.OperatingEntity;
import com.ls.lserp.modules.setting.entity.OperatingRREntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly = false)
public class CheckProjectService extends CrudService<CheckProjectDao,CheckProjectEntity>{
    
    @Autowired
    private CheckProjectDao checkProjectDao ;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDao orderDao;
    /**
	 * 订单项目名称查询
	 * @param checkProjectEntity
	 * @return checkProjectEntity
	 */
    public CheckProjectEntity selectValueNameCnList(CheckProjectEntity checkProjectEntity) {
		// 款式设计LIst
		List<CheckProjectEntity> listOne = checkProjectDao.selectValueNameCnListOne ();
		// 辅料List
		List<CheckProjectEntity> listTwo = checkProjectDao.selectValueNameCnListTwo ();
		//标牌位置
		List<CheckProjectEntity> listFour = checkProjectDao.selectValueNameCnListFour ();
		// 刺绣
		List<CheckProjectEntity> listFive = checkProjectDao.selectValueNameCnListFive ();
		// 配料
		List<CheckProjectEntity> listSix = checkProjectDao.selectValueNameCnListSix ();
		// 量体
		List<CheckProjectEntity> listSeven = checkProjectDao.selectValueNameCnListSeven ();
		// 工序list
		List<OperatingEntity> listOperatingList = checkProjectDao.selectValueNameCn ();
		List<OperatingRREntity> operatingREntityList = checkProjectDao.selectOpId (checkProjectEntity);
		List<CheckProjectEntity> operatingFitREntityList = checkProjectDao.selectOpFitId(checkProjectEntity);
		checkProjectEntity.setTypeCdListOne (listOne);
		checkProjectEntity.setTypeCdListTwo (listTwo);
		checkProjectEntity.setTypeCdListFour (listFour);
		checkProjectEntity.setTypeCdListFive (listFive);
		checkProjectEntity.setTypeCdListSix (listSix);
		checkProjectEntity.setTypeCdListSeven(listSeven);
		checkProjectEntity.setOperatingList (listOperatingList);
		checkProjectEntity.setOperatingREntityList (operatingREntityList);
		checkProjectEntity.setOperatingFitREntityList (operatingFitREntityList);
		return checkProjectEntity;
    }
	/**
	 * 导出项目设置
	 * @param checkProjectEntity
	 * @return
	 * @throws Exception 
	 */
    public void insertDesId(CheckProjectEntity checkProjectEntity) throws Exception {
		List<CheckProjectEntity> allDataList = allData(checkProjectEntity);
		checkProjectEntity.setTypeCdListOne (allDataList);
		checkProjectDao.deleteOperating (checkProjectEntity);
		checkProjectDao.deleteOperatingFit(checkProjectEntity);
		if(checkProjectEntity.getTypeCdListOne ().size () > 0) {
			checkProjectDao.insertOperating (checkProjectEntity);
		}
		List<CheckProjectEntity> allFitDataList = new ArrayList<>();
		CheckProjectEntity fitTemp = new CheckProjectEntity();
		if (checkProjectEntity.getTypeCdListSeven() != null && checkProjectEntity.getTypeCdListSeven().size() > 0) {
			for (CheckProjectEntity item:checkProjectEntity.getTypeCdListSeven()){
				if (item.getFitPropertyId ()!=null && Integer.parseInt (item.getFitPropertyId()) > 0) {
					fitTemp = new CheckProjectEntity();
					fitTemp.setFitPropertyId (item.getFitPropertyId ());
					allFitDataList.add (fitTemp);
				}
			}
			checkProjectEntity.setTypeCdListSeven(allFitDataList);
			checkProjectDao.insertOperatingFit (checkProjectEntity);
		}
		System.out.println("新工序数据处理开始----"+new Date());
		// 工序实体类
		OperatingEntity operatingEntity = new OperatingEntity();
		// 设置新工序id
		operatingEntity.setOperatingId(Long.parseLong(checkProjectEntity.getOperatingId()));
		// 订单实体类
		Order order = new Order();
		// 合同实体类
		CustomizationContract customizationContract = new CustomizationContract();
		// 设置 合同状态 为 生产中
		customizationContract.setOrderStatus(ResourcesUtil.getConfig("Workshop_30"));
		//获取工序下量体code
        List<Order> fitList = orderService.findFitCodeList (order);
        //通过合同ID获取订单集
        List<Order> orderList = orderService.findOrderList(order);
		List<CustomizationContract> contractIdList = orderDao.findContractIdList(customizationContract);
		if (contractIdList != null && contractIdList.size() > 0) {
			for(CustomizationContract contract : contractIdList){
				order.setContractID(contract.getId()+"");
				orderService.updateOperatingData(order,operatingEntity,fitList,orderList);
			}
		}
		System.out.println("新工序数据处理结束----"+new Date());

	}
    
    /**
	 * 获取工序最小ID
	 * @return
	 */
    public String getOperatingMinId() {
    	return checkProjectDao.getOperatingMinId();
	}

	public List<CheckProjectEntity> allData(CheckProjectEntity checkProjectEntity){
		List<CheckProjectEntity> allDataList = new ArrayList <> ();
		CheckProjectEntity projectEntityTemp;
		// 款式设计LIst
		List<CheckProjectEntity> listOne = checkProjectEntity.getTypeCdListOne ();
		// 辅料List
		List<CheckProjectEntity> listTwo = checkProjectEntity.getTypeCdListTwo ();
		// 标牌位置
		List<CheckProjectEntity> listFour = checkProjectEntity.getTypeCdListFour ();
		// 刺绣
		List<CheckProjectEntity> listFive = checkProjectEntity.getTypeCdListFive ();
		// 配料
		List<CheckProjectEntity> listSix = checkProjectEntity.getTypeCdListSix ();
		for (CheckProjectEntity item:listOne){
            if (item.getDesignopionId ()!=null && Integer.parseInt (item.getDesignopionId ()) > 0) {
				projectEntityTemp = new CheckProjectEntity();
				projectEntityTemp.setDesignopionId (item.getDesignopionId ());
				allDataList.add (projectEntityTemp);
			}
		}
		for (CheckProjectEntity item:listTwo){
            if (item.getDesignopionId ()!=null && Integer.parseInt (item.getDesignopionId ()) > 0) {
				projectEntityTemp = new CheckProjectEntity();
				projectEntityTemp.setDesignopionId (item.getDesignopionId ());
				allDataList.add (projectEntityTemp);

			}
		}
		for (CheckProjectEntity item:listFour){
            if (item.getDesignopionId ()!=null && Integer.parseInt (item.getDesignopionId ()) > 0) {
				projectEntityTemp = new CheckProjectEntity();
				projectEntityTemp.setDesignopionId (item.getDesignopionId ());
				allDataList.add (projectEntityTemp);
			}
		}
		for (CheckProjectEntity item:listFive){
            if (item.getDesignopionId ()!=null && Integer.parseInt (item.getDesignopionId ()) > 0) {
				projectEntityTemp = new CheckProjectEntity();
				projectEntityTemp.setDesignopionId (item.getDesignopionId ());
				allDataList.add (projectEntityTemp);
			}
		}
		for (CheckProjectEntity item:listSix){
			if (item.getDesignopionId ()!=null && Integer.parseInt (item.getDesignopionId ()) > 0) {
				projectEntityTemp = new CheckProjectEntity();
				projectEntityTemp.setDesignopionId (item.getDesignopionId ());
				allDataList.add (projectEntityTemp);
			}
		}
		return allDataList;
	}
	
	/**
	 * 
	 * @param operatingId
	 * @return
	 */
	public Map<String,Object> findItemByOperating(Long operatingId){
		Map<String,Object> map = new HashMap();
		List<OperatingRREntity> partLeft = new ArrayList<OperatingRREntity>();
		List<OperatingRREntity> partRight = new ArrayList<OperatingRREntity>();
		List<OperatingRREntity> itemList = checkProjectDao.findItemList(operatingId);
		if (itemList != null && itemList.size() > 0) {
			for (int i = 0; i < itemList.size(); i++) {
				if ((i+1)%2 == 1) {
					partLeft.add(itemList.get(i));
				}else {
					partRight.add(itemList.get(i));
				}
			}
		}
		map.put("partLeft", partLeft);
		map.put("partRight", partRight);
		return map;
	}
	
	/**
	 * 
	 * @param operatingREntity
	 */
	public void saveOperatingSortData(OperatingRREntity operatingREntity){
		if (operatingREntity.getOperatingRList() != null) {
			for(OperatingRREntity oEntity : operatingREntity.getOperatingRList()){
				if ("DESIGN".equals(oEntity.getItemFlag())) {
					checkProjectDao.updateDesignR(oEntity);
				}else if ("FIT".equals(oEntity.getItemFlag())) {
					checkProjectDao.updateFitR(oEntity);
				}
			}
		}
	}
}    